﻿using StudentLibrary.Module;
using StudentLibrary.Module.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SQLite;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace StudentLibrary
{
    public partial class FormAddIssueRefund : Form
    {
        public FormAddIssueRefund()
        {
            InitializeComponent();
        }

        private void buttonIR_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(textBoxidBook.Text) ||
                    string.IsNullOrWhiteSpace(textBoxFio.Text))
                {
                    MessageBox.Show("Есть пустые поля!", "Внимание", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                    return;
                }

                var id = int.Parse(textBoxidBook.Text);

                if (!Book.IsExist(id))
                {
                    MessageBox.Show("Книги с таким ИД не существует!", "Внимание", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                    return;
                }

                var sql = $@"INSERT INTO IssueRefund (idBook, fio, dateOfIssue, dateOfReturn)
                             VALUES (@id, @fio, @dateofissue, @dateofreturn)";

                var isCreate = sql.SQLNoneQueryWithParametrs(new Dictionary<string, string>()
                {
                    ["@id"] = textBoxidBook.Text,
                    ["@fio"] = textBoxFio.Text,
                    ["@dateofissue"] = ((DateTimeOffset)dateTimePickerDOI.Value.Date).ToUnixTimeSeconds().ToString(),
                    ["@dateofreturn"] = ((DateTimeOffset)dateTimePickerDOR.Value.Date).ToUnixTimeSeconds().ToString()
                });

                if (isCreate)
                {
                    MessageBox.Show("Успешно!", "Внимание", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Close();
                }
            }
            catch (SQLiteException sqle)
            {
                MessageBox.Show($"Ошибка: {sqle.Message}");
            }
        }

        private void textBoxidBook_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }
        }
    }
}
